//
//  BLHotelshareViewController.m
//  blanket
//
//  Created by Mac on 2017/12/6.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLHotelshareViewController.h"
#import "STRadioButton.h"
#import "BLHotelShareModel.h"
#import "BLClothRecicalPresentViewController.h"
@interface BLHotelshareViewController ()
@property(nonatomic, strong) STRadioButton                     *qingjieRadio;
@property(nonatomic, strong) STRadioButton                     *fuwueRadio;
@property(nonatomic, strong) STRadioButton                     *feiyongRadio;
@property(nonatomic, strong) NSMutableArray <BLHotelShareModel*>                    *dataSouce;
@property(nonatomic, strong) NSMutableArray  <STRadioButton*>                   *buttonArrays;
@end

@implementation BLHotelshareViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"酒店布草分享";
    self.tableView.backgroundColor = [UIColor whiteColor];
    // [self configSubView];
    
    
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self sendQuestionRequest];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(30, 15, UIScreenWidth - 60, 20)
                                                     text:@"1.您对酒店布草的清洁程度要求高还是低?"
                                                textColor:BL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    self.qingjieRadio = [[STRadioButton alloc] initWithFrame:CGRectMake(30, titleLable.bottom + 10, 100, 80) verticalRadio:@[@"低",@"高"]];
    [header addSubview:self.qingjieRadio];
    
    STLabel * titleLable1 = [[STLabel alloc] initWithFrame:CGRectMake(30, self.qingjieRadio.bottom, UIScreenWidth - 60, 20)
                                                     text:@"2.您对铺盖科技的服务满意吗?"
                                                textColor:BL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable1];
    self.fuwueRadio = [[STRadioButton alloc] initWithFrame:CGRectMake(30, titleLable1.bottom + 10, 100, 80) verticalRadio:@[@"不满意",@"满意"]];
    [header addSubview:self.fuwueRadio];
    
    STLabel * titleLable2 = [[STLabel alloc] initWithFrame:CGRectMake(30, self.fuwueRadio.bottom, UIScreenWidth - 60, 20)
                                                      text:@"3.您觉得平台收取的费用是否合理?"
                                                 textColor:BL_firstTextColor
                                                      font:14
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable2];
    self.feiyongRadio = [[STRadioButton alloc] initWithFrame:CGRectMake(30, titleLable2.bottom + 10, 100, 80) verticalRadio:@[@"不合理",@"合理"]];
    [header addSubview:self.feiyongRadio];
    
    __weak typeof(self) weakSelf =  self;

    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"提交"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.top = self.feiyongRadio.bottom + 30;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [header addSubview:confimButton];
    
    header.height = confimButton.bottom;
    self.tableView.tableHeaderView = header;

}
- (UIView*)viewWithModel:(BLHotelShareModel*)model{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(30, 15, UIScreenWidth - 60, 20)
                                                     text:[NSString stringWithFormat:@"%@?",model.title]
                                                textColor:BL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [view addSubview:titleLable];
    [NSString lableAutoAdjustheightWithLabel:titleLable];
    STRadioButton * qingjieRadio = [[STRadioButton alloc] initWithFrame:CGRectMake(30, titleLable.bottom + 10, 100, 80) verticalRadio:@[model.one,model.two]];
    [view addSubview:qingjieRadio];
    qingjieRadio.tag = 10000;
    model.radioButton = qingjieRadio;
    view.height = qingjieRadio.bottom ;
    return view;
}
- (void)reloadData{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    CGFloat top = 0;
    for (BLHotelShareModel * model in self.dataSouce) {
        UIView * view = [self viewWithModel:model];
        view.top = top;
        [header addSubview:view];
//        STRadioButton * button = [view viewWithTag:10000];
        top = view.bottom;
    }

    
    __weak typeof(self) weakSelf =  self;
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)
                                                        title:@"提交"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:5
                                              backgroundColor:BL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.top = top + 30;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [header addSubview:confimButton];
    
    header.height = confimButton.bottom;
    self.tableView.tableHeaderView = header;
}
- (void)onSelectedCommitButton{
    for (BLHotelShareModel * model in self.dataSouce) {
        if (!model.radioButton.checkedString.length) {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"您还未选择第%ld个问题的答案",(model.index + 1)]];
            return;
        }
    }
    //提交
    NSMutableArray * answerArray = [NSMutableArray new];
    for (BLHotelShareModel * model  in self.dataSouce) {
        NSString * qustionID = model.shareID;
        
        NSNumber *num = model.radioButton.checkedNum;
        num  = [NSNumber numberWithInteger:num.integerValue + 1];
        if (qustionID.length && num) {
            NSDictionary * dic = @{qustionID:num};
            [answerArray addObject:dic];
        }else{
          [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"您还未选择第%ld个问题的答案",(model.index + 1)]];
            return;
        }
        
    }
    BLClothRecicalPresentViewController * vc = [BLClothRecicalPresentViewController new];
    vc.isHotolShare = YES;
    vc.anwserArray = answerArray;
    [self presentViewController:vc animated:NO completion:nil];
}

#pragma mark --NetWork Method
- (void)sendQuestionRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/getProblemAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }

    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * dataArray =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 self.dataSouce = [NSMutableArray new];
                                                 int i = 0;
                                                 for (NSDictionary * modelDic in dataArray) {
                                                     BLHotelShareModel * model = [BLHotelShareModel mj_objectWithKeyValues:modelDic];
                                                     model.index = i;
                                                     [self.dataSouce addObject:model];
                                                     i ++;
                                                 }
                                                 [self reloadData];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
